<?php
	//Get a detailed_payroll
	function get_payroll_info($payroll_id=-1)
	{		
		$payroll_info = $this->Staff->get_payroll_for_editing($payroll_id);
		$payroll = $payroll_info['query_payroll']->row();
		$date = $payroll->date;
		$year = $payroll->year;
		$month = $payroll->month;
		$day = $payroll->day;
		$tax_cat = $payroll->tax_cat;
		$ssf_cat = $payroll->ssf_cat;
		$basic_wages = $payroll->basic_wages;
		$staff_id = $payroll->person_id;
		
		//Get the total  allawance
		$allowance_result = $this->Staff->get_allowance_totals($payroll_id);
		if($allowance_result->num_rows() > 0)
		{
			$allowance_row = $allowance_result->row();
			$allowance_totals = $allowance_row->payroll_total;
			if(empty($allowance_totals))
			{
				$allowance_totals = 0;
			}
		}
		else
		{
			$allowance_totals = 0;
		}
		$allowance_totals = number_format($allowance_totals, 2, '.', '');
			
			///////Get the total deductions//////
		$deductions_result = $this->Staff->get_deduction_totals($payroll_id);
		if($deductions_result->num_rows() > 0)
		{
			$deductions_row = $deductions_result->row();
			$deduction_totals = $deductions_row->payroll_total;
			if(empty($deduction_totals))
			{
				$deduction_totals = 0;
			}
		}
		else
		{
			$deduction_totals = 0;
		}
		$deduction_totals = number_format($deduction_totals, 2, '.', '');
		
		////////////*************************** Now Do Tax and SS and Others**************************** ////////////
		
			$all_calc = $this->all_calc($basic_wages,$allowance_totals,$ssf_cat,$tax_cat,$deduction_totals);
			
			/////////////////////////////////////////********************************************////////////////////////////////////\
			$data['staff_id'] = $staff_id;
			$data['payroll_id'] = $payroll_id;
			$data['date'] = $date;
			$data['basic_wages'] = $basic_wages;
			$data['allowance_totals'] = $allowance_totals;
			
			$data['ssf_array'] = $all_calc['ssf_array'];
			
			$data['net_basic_wage'] = $all_calc['net_basic_wage'];
			$data['gross_pay'] = $all_calc['gross_pay'];
			$data['tax'] = $all_calc['tax'];
			$data['taxable_income'] = $all_calc['taxable_income'];
			$data['deduction_totals'] = $deduction_totals;
			$data['net_pay'] = $all_calc['net_pay'];
			$data['total_payroll_deductions'] = $all_calc['total_payroll_deductions'];
			$data['total_ssf_payable'] = $all_calc['total_ssf_payable'];
			$data['default_ssf_percent_value'] = $all_calc['default_ssf_percent_value'];
			$data['default_ssf_value'] = $all_calc['default_ssf_value'];
			$data['ssf_percent_value_pair'] = $all_calc['ssf_percent_value_pair'];
			
				
		return $data;
	}
	
	function detailed_payroll($payroll_id)
	{
		$data = array();
		$data = $this->get_payroll_info($payroll_id);
		$payroll_info = $this->Staff->get_payroll_for_editing($payroll_id);
		//Allowance
		foreach($payroll_info['query_allowance']->result_array() as $row)
		{
			$allowance[$row['name']] = $row['amount'] ; 	
		}
		//Deductions
		foreach($payroll_info['query_deductions']->result_array() as $row)
		{
			$deductions[$row['name']] = $row['amount'] ; 	
		}
		
		$data['allowance']=$allowance;
		$data['deductions']=$deductions;
		$this->load->view("staffs/detailed_payroll", $data);
	}
	
	//To print a payroll for a month
	function print_payroll($payroll_id)
	{
		$data = array();
		$data = $this->get_payroll_info($payroll_id);
		$data['person_info']=$this->Staff->get_info($data['staff_id']);
		$payroll_info = $this->Staff->get_payroll_for_editing($payroll_id);
		//Allowance
		foreach($payroll_info['query_allowance']->result_array() as $row)
		{
			$allowance[$row['name']] = $row['amount'] ; 	
		}
		//Deductions
		foreach($payroll_info['query_deductions']->result_array() as $row)
		{
			$deductions[$row['name']] = $row['amount'] ; 	
		}
		
		$data['allowance']=$allowance;
		$data['deductions']=$deductions;
		$data['controller']=strtolower($this->uri->segment(1));
		
		$this->load->view("staffs/print_payroll", $data);
	}
	
	
	///////////////////CALCULATING FUNCTIONS///////////////////////////
	
	//CAll the calculating functions
	function all_calc($basic_wages,$allowance_totals,$ssf_cat,$tax_cat,$deduction_totals)
	{
		$result = array();
		//Get the default ssf percent value
		$default_ssf_value = 			$this->cal_default_ssf($ssf_cat,$basic_wages);
		//Calc Gross Pay
		$gross_pay = 					$this->cal_gross_pay($basic_wages,$allowance_totals);
		//Calc Net Basic Wage
		$net_basic_wage = 				$this->cal_net_basic_wage($basic_wages,$default_ssf_value);
		//Calc SSF
		$ssf_array = 					$this->cal_ssf($ssf_cat,$basic_wages);
		
		//Taxable Income
		$taxable_income = 				$this->cal_taxable_income($gross_pay,$default_ssf_value);
		//Tax
		$tax = 							$this->cal_tax($tax_cat,$taxable_income);
		//Total Deductions
		$total_payroll_deductions = 	$this->cal_total_payroll_deductions($tax,$deduction_totals);
		//Net Pay
		$net_pay = 						$this->cal_net_pay($taxable_income,$total_payroll_deductions);
		
		//Total SSF Payable
		$total_ssf_payable = 			$this->cal_total_ssf_payable($ssf_array);
		
		//Get the default ssf percent value
		$default_ssf_percent_value = 	$this->default_ssf_percent_value($ssf_cat);
		
		
		
		//SSFs Percent and Value Pair
		$ssf_percent_value_pair = 		$this->cal_ssf_percent_value_pair($ssf_cat,$basic_wages);
		
		
		$result['gross_pay'] = 	$gross_pay;				
		$result['net_basic_wage'] = $net_basic_wage	;		
		$result['ssf_array'] = 	$ssf_array;			
		$result['taxable_income'] = $taxable_income	;			
		$result['tax'] = 	$tax;						
		$result['net_pay'] = 	$net_pay;					
		$result['total_payroll_deductions'] = 	$total_payroll_deductions;
		$result['total_ssf_payable'] = 	$total_ssf_payable;
		$result['default_ssf_percent_value'] = 	$default_ssf_percent_value;
		$result['default_ssf_value'] = 	$default_ssf_value;
		$result['ssf_percent_value_pair'] = 	$ssf_percent_value_pair;

		return $result;
	}
	
	//Calc SSF
	function cal_ssf($ssf_cat,$gross_pay)
	{			
		//SSFs
		if(is_numeric($ssf_cat))
		{
			//Get ssf Components
			$ssf_items = $this->SSF_config->get_ssf_coponents($ssf_cat);
			if($ssf_items->num_rows() > 0)
			{
				$ssf_comp_array = array();
				foreach($ssf_items->result() as $row)
				{
					//$row->order
					$percent_value = $row->percent_value;
					$ssf_comp_array[] = ($gross_pay*($percent_value/100));
				}
				return $ssf_comp_array;
			}
			else
			{
				return 0;
			}
		}
		else
		{
			return 0;
		}
		
	}
	
	//SSFs Percent and Value Pair
	function cal_ssf_percent_value_pair($ssf_cat,$gross_pay)
	{			
		//SSFs
		if(is_numeric($ssf_cat))
		{
			//Get ssf Components
			$ssf_items = $this->SSF_config->get_ssf_coponents($ssf_cat);
			if($ssf_items->num_rows() > 0)
			{
				$ssf_comp_array = array();
				foreach($ssf_items->result() as $row)
				{
					//$row->order
					$percent_value = $row->percent_value;
					$ssf_comp_array[$percent_value] = ($gross_pay*($percent_value/100));
				}
				return $ssf_comp_array;
			}
			else
			{
				return 0;
			}
		}
		else
		{
			return 0;
		}
		
	}
	
	//Default ssf info
	function default_ssf_percent_value($ssf_cat)
	{			
		if(is_numeric($ssf_cat))
		{
			//Net Basic Wage
			$ssf = $this->SSF_config->get_info($ssf_cat);
			$ssf_net_wage = $ssf->start; //Geting the dedault ssf value to cal the net wage
			$query_row = $this->SSF_config->get_default_ssf($ssf_net_wage);
			$percent_netwage = 0;
			$percent_value = 0;
			if($query_row)// Default exist 
			{
				$percent_value = $query_row->percent_value;
				return $percent_value;
			}
			else // Use next available value
			{
				$ssf_items = $this->SSF_config->get_ssf_coponents($ssf_cat);
				if($ssf_items->num_rows() > 0)
				{
					foreach($ssf_items->result() as $row)
					{		
						$percent_value = $row->percent_value;
						break;						
					}
					return $percent_value;
				}
				else
				{
					return 0;
				}
			}
		}
		else
		{
			return 0;
		}
		
	}
	
	//Getting the default SSF value
	function cal_default_ssf($ssf_cat,$gross_pay)
	{			
		if(is_numeric($ssf_cat))
		{
			//Net Basic Wage
			$ssf = $this->SSF_config->get_info($ssf_cat);
			$ssf_net_wage = $ssf->start; //Geting the dedault ssf value to cal the net wage
			$query_row = $this->SSF_config->get_default_ssf($ssf_net_wage);
			$percent_netwage = 0;
			$percent_value = 0;
			if($query_row)// Default exist 
			{
				$percent_value = $query_row->percent_value;
				$percent_netwage = ($gross_pay*($percent_value/100));
				return $percent_netwage;
			}
			else // Use next available value
			{
				$ssf_items = $this->SSF_config->get_ssf_coponents($ssf_cat);
				if($ssf_items->num_rows() > 0)
				{
					foreach($ssf_items->result() as $row)
					{		
						$percent_value = $row->percent_value;
						$percent_netwage = ($gross_pay*($percent_value/100));
						break;						
					}
					return $percent_netwage;
				}
				else
				{
					return 0;
				}
			}
		}
		else
		{
			return 0;
		}
	}
	
	
	//Calc Net Basic Wage
	function cal_net_basic_wage($basic_wages,$default_ssf_value)
	{			
		//Net Basic Wage
		$net_basic_wage = $basic_wages - $default_ssf_value;
		
		return $net_basic_wage;
	}
	
	
	//Calc Gross Pay
	function cal_gross_pay($basic_wages,$total_allowances)
	{					
		return $basic_wages + $total_allowances;
	}
	
	//Tax
	function cal_tax($tax_cat,$taxable_income)
	{
		if(is_numeric($tax_cat))
		{
			$tax = $this->Tax_config->get_info($tax_cat);
			$exceeding = $tax->exceeding; 
		
			$tax_items = $this->Tax_config->get_tax_coponents($tax_cat);
			if($tax_items->num_rows() > 0)
			{
				if($taxable_income >= $tax_items->row()->amount)//Taxable income must be grater than the very first entry
				{
					$percent_tax = array();
					$percent_value = 0;
					foreach($tax_items->result() as $row)
					{
						$row_amount = $row->amount;
						if($row_amount <= $taxable_income)
						{
							$percent_value = $row->percent_value;
							$percent_tax[] = ($row_amount*($percent_value/100));
							$taxable_income = $taxable_income - $row_amount;
						}
						else
						{
							$percent_value = $row->percent_value;
							$percent_tax[] = ($taxable_income*($percent_value/100));
							$taxable_income = $taxable_income - $taxable_income;
							break;
						}
					}
					// Now Exceeding
					if($taxable_income > 0)
					{
						$percent_tax[] = ($taxable_income*($exceeding/100));
					}
		
					//Sum them all
					return number_format(array_sum($percent_tax), 2, '.', '');
				}
				else // No tax At all
				{
					return 0;
				}
			}
			else // Only Exceeding 
			{
				if($taxable_income > 0)
				{
					$only_exceeding = ($taxable_income*($exceeding/100));
				}
				return number_format($only_exceeding, 2, '.', '');
			}
		}
		else
		{
			return 0;
		}
		
	}
	
	//Taxable Income
	function cal_taxable_income($gross_pay,$default_ssf_value)
	{					
		return $gross_pay - $default_ssf_value;
	}
	
	//Net Pay
	function cal_net_pay($taxable_income,$total_payroll_deductions)
	{					
		return $taxable_income - $total_payroll_deductions;
	}
	
	//total_payroll_deductions
	function cal_total_payroll_deductions($tax,$deduction_totals)
	{					
		return  $tax + $deduction_totals;
	}
	
	//Total SSF Payable
	function cal_total_ssf_payable($ssf_array)
	{					
		if(is_array($ssf_array))
		{
			return array_sum($ssf_array);
		}
		else
		{
			return $ssf_array;
		}
		
	}
	
	/////////////////////////////////*************************************////////////////////////////////////
?>